#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime
import json

from flask import Module, Response, request, flash, jsonify, g, current_app,\
    abort, redirect, url_for, session, Blueprint, render_template

from werkzeug import secure_filename

from planet.file import backend
# from planet.file.forms import PostForm
from planet.file.models import File
from planet.extensions import db, cache, photos
from planet.helpers import save_image


@backend.route("/", methods=["GET"])
def index():
    page = int(request.args.get('p', 1))
    files = File.query.paginate(page, per_page=50)
    return render_template("file/index.html", page_obj=files)


@backend.route("/upload", methods=["POST"])
def upload():
    if 'upload_file' in request.files:
        print 'here2'
        file = save_image(request.files['upload_file'])
        # return files.url(file)
        return jsonify(file_path=photos.url(file))
    # return json.dumps({
    #                   'file_path': files.url(file)})
